<?php

class BayesFeatureSet implements Iterator
{
	
	protected $features_array = array();
	
	protected $position = 0;
	
	protected $feature_model  = NULL;
	
	// Create features that dont exist from the feature set and increment counters by one
	public function increment_counters()
	{
		foreach($this->features_array as $feature)
		{
			
			if(!$feature->exists())
			{
				$feature->create();
			}
			
			$feature->increment_counter();
			
		}
	}
	
	public function load_features_to_object_from_array($array)
	{
		// reset features array
		$this->features_array = array();
		foreach($array as $text)
		{
			
			$arguments = array(	$this->feature_model->get_table_name(), 
								$this->feature_model->get_db_name(),
								$key = array('feature_text' => $text));
			
			$this->features_array[] = BayesDataTemplate::factory('Bayes_feature_data_Model', $arguments);
		}
		
		return $this->features_array;
	}
	
	public function __construct(Bayes_feature_data_Model $feature_model, $features_array = array())
	{
		$this->feature_model = $feature_model;
		if(!empty($features_array))
		{
			$this->load_features_to_object_from_array($features_array);
		}
	}
	
	
	function rewind() {
        
        $this->position = 0;
    }

    function current() {
        
        return $this->features_array[$this->position];
    }

    function key() {
        
        return $this->position;
    }

    function next() {
       
        ++$this->position;
    }

    function valid() {
        
        return isset($this->features_array[$this->position]);
    }
	
	
}
